#include<iostream>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>

void Myperror_(const char* s)
{
    printf("%s : %s\n", s, strerror(errno));
}

int main()
{
    //int fd = open("log.txt", O_RDONLY);
    //if(fd < 0)
    //{
    //    //perror("open");
    //    Myperror_("open");
    //    return 1;
    //}
    
    //errno = 1;
    //perror("snowdragon wirted");

    //printf("snowdragon writed, printf 1\n");
    //fprintf(stdout, "snowdragon writed fprintf, 1\n");
    ////int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    //perror("snowdragon stderr 2");

    //std::cout << "snowdraon cout" << std::endl;
    //std::cerr << "snowdragon cerr 2" << std::endl;

    

    close(1);
    printf("snowdragon writed, printf\n");
    fprintf(stdout, "snowdragon writed, fprintf\n");
    int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    
    //wirte在不启用下方的fflush语句时也可以正常显示
    const char* s = "snowdragon writed\n";
    write(fd, s, strlen(s));
    
    //fflush(stdout);
    close(fd);

    return 0;
}
